package org.hbbTlias2025.controller;


import lombok.extern.slf4j.Slf4j;
import org.hbbTlias2025.pojo.Result;
import org.hbbTlias2025.pojo.Student;
import org.hbbTlias2025.pojo.StudentQueryParam;
import org.hbbTlias2025.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;


    @DeleteMapping("/{ids}")
    public Result deleteStudentByList(@PathVariable List<Integer> ids) {
        log.info("现在删除学生，好多学生信息信息——————{}", ids);
        studentService.deleteIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result getStudent(StudentQueryParam studentQueryParam){
        log.info("查询所有学生");
        try {
            log.info("查询所有学生,信息如下{}", studentQueryParam);
            return Result.success(studentService.getAllStudent(studentQueryParam));
        }catch (Exception e){
            return Result.error("查询学生异常我抄__" + e.getMessage());
        }
    }


    @GetMapping("/{id}")
    public Result getStudentById(@PathVariable Integer id){
        log.info("根据班级id查询学生");
        try {
            log.info("根据班级id查询学生,信息如下{}", id);
            return Result.success(studentService.getStudentById(id));
        }catch (Exception e){
            return Result.error("查询学生异常我抄__" + e.getMessage());
        }
    }

    @PutMapping
    public Result updateOneStudent(@RequestBody Student student){
        log.info("修改学生信息");
        try {
            log.info("修改学生信息,信息如下{}", student);
            studentService.updateStudent(student);
            return Result.success("修改成功");
        }catch (Exception e){
            log.info("修改学生异常我抄__{}", e.getMessage());
            return Result.error("修改学生异常我抄__" + e.getMessage());
        }
    }


    @PutMapping("/violation/{id}/{score}")
    public Result addStudentViolation(@PathVariable Integer id, @PathVariable Integer score){
        log.info("添加学生违纪我的天aaa");
        try {
            log.info("根据班级id查询学生,然后顺手添加违纪,信息如下{}", id);
            studentService.addStudentViolation(id, score);
            return Result.success();
        }catch (Exception e){
            return Result.error("查询并添加违纪学生异常我抄__" + e.getMessage());
        }
    }

    @PostMapping
    public Result addStudent(@RequestBody Student student){
        log.info("添加学生信息");
        try {
            log.info("添加学生信息,信息如下{}", student);

            studentService.addStudent(student);

            return Result.success("添加成功");

        } catch (Exception e){
            return Result.error("添加学生异常我抄__" + e.getMessage());
        }
    }

}
